﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;
using System.Runtime.InteropServices;
using PDAMain.Entity;

namespace PDAMain.Forms.CustomerWork
{
    public partial class CustomerToRepair : BaseForm
    {
        public string customerID;
        private bool scanFlag = true;
        private string showFlag = "";
        private bool tpFlag = true; //照相标记，控制是否能够保存
        public CustomerToRepair()
        {
            InitializeComponent();
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            if (scanFlag)
            {
                MessageBox.Show("请扫描对应资产编号");
                return;
            }
            //拍照
            if (tpFlag)
            {
                MessageBox.Show("请先拍照！");
                return;
            }
            if (comRepairInfor.SelectedIndex==0)
            {
                MessageBox.Show("请选择报修原因");
                return;
            }
            string reason = comRepairInfor.SelectedText.ToString().Trim();
            string strRemark = remark.Text.ToString().Trim();
            string applyTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            if (DataHelper.ApplyToRepairDao.insertApplyRepair(lblAssetNo.Text, Identity.dutyPerson, applyTime, strRemark, reason,"")
                && DataHelper.CustomerWorkDao.updateWorkOrderStatus(customerID, "9", "", applyTime))
            {
                DataHelper.CustomerWorkDao.deleteLocalData(customerID);
                MessageBox.Show("成功同步数据至远程数据库");
            }
            else 
            {
                bool result1 = DataHelper.ApplyToRepairDao.insertLacolTable(lblAssetNo.Text, Identity.dutyPerson, applyTime, strRemark, reason, "1", "","");
                bool result2 = DataHelper.CustomerWorkDao.insertLacolTable(customerID, "9", applyTime, strRemark);
                if (result1 && result2)
                {
                    MessageBox.Show("同步数据失败，保存至本地数据库");
                }
                else
                {
                    MessageBox.Show("保存失败");
                }
            }
            backToList();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            //返回
            CustomerDetail detail = new CustomerDetail();
            detail.customerID = customerID;
            detail.Show();
            this.Close();
        }

        private void CustomerToRepair_Load(object sender, EventArgs e)
        {
            // 打开BarCode扫描设备
            if (!OpenBarCode())
            {
                /*
                int ErrCode = Marshal.GetLastWin32Error();
                MessageBox.Show("Fail to Open BarCode :(, ErrCode = " + ErrCode.ToString(), "Error");
                 */
                MessageBox.Show("打开扫描接口失败，请重登系统");
            }

            EnableBeeper(true);
            EnableVibrater(true);

            BindData();
        }

        // 重载 基类BaseForm的这个方法
        public override void OnBarCodeNotify(byte[] BarCodeData, int nLength)
        {
            EnableBarCode(false);

            string BarCode = Encoding.Default.GetString(BarCodeData, 0, nLength);
            lblAssetNo.Text = BarCode;

            //scanFlag置为false后才能保存本窗体数据
            scanFlag = false;
            EnableBarCode(true);
        }

        private void BindData()
        {
         

            //綁定下拉框數據
            applyPerson.Text = Identity.dutyPerson;
            comRepairInfor.Items.Clear();
            comRepairInfor.Items.Insert(0, "--请选择--");
            comRepairInfor.Items.Insert(1, "报修原因1");
            comRepairInfor.Items.Insert(2, "报修原因2");
            comRepairInfor.SelectedIndex = 0;
        }

        private void CustomerToRepair_Closed(object sender, EventArgs e)
        {
            // 关闭BarCode扫描设备
            CloseBarCode();
        }

        private void backToList()
        {
            CustomerList list = new CustomerList();
            list.Show();
            this.Close();
        }

        //拍照
        private void btn_Click(object sender, EventArgs e)
        {
            string folderName = "Customer";
            string strAssetNo = lblAssetNo.Text.ToString();
            if (strAssetNo == string.Empty)
            {
                MessageBox.Show("请先扫描资产编号");
                return;
            }
            TakePicture tp = new TakePicture();
            string strFileName = "";
            try
            {
                //维修前showFlag为空,维修中为1，维修后为2
                if (showFlag == string.Empty)
                {
                    strFileName = customerID + "_" + DateTime.Now.ToString("yyyyMMddHHmmss");
                    pb2.Image = System.Drawing.Image.FromHbitmap(tp.Show(folderName, strFileName).GetHbitmap());
                    pb2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                    tpFlag = false;
                }

            }
            catch
            {
                tp.dispose();
            }
            finally
            {
                tp.dispose();
            }
        }
    }
}